# 指定 CMake 的最低版本要求
cmake_minimum_required(VERSION 3.18)

# 定义项目的名称和使用的编程语言
project(mysql-demo C)

# 添加子目录
add_subdirectory(src)

# 指定要生成的可执行文件和其源文件：add_executable(<target> <source_files>...)
add_executable(main main.c)
target_compile_features(main PRIVATE cxx_std_17)

# 链接所有模块的目标
target_link_libraries(main PRIVATE tools test)

# 添加头文件搜索路径
include_directories(include)
include_directories(/nix/store/i1vgb2a2va9phkxp1nnb4myp099dwq42-mysql-8.0.40/include/mysql/)

# 查找 MySQL 配置工具
find_program(MYSQL_CONFIG mysql_config)
message(STATUS "mysql_config is ${MYSQL_CONFIG}")
if(NOT MYSQL_CONFIG)
    message(FATAL_ERROR "mysql_config not found! Please install MySQL or set MYSQL_CONFIG environment variable.")
endif()
# 使用 execute_process 获取 MySQL 编译和链接标志
execute_process(
    COMMAND ${MYSQL_CONFIG} --cflags
    OUTPUT_VARIABLE MYSQL_CFLAGS
    OUTPUT_STRIP_TRAILING_WHITESPACE
)
message(STATUS "MYSQL_CFLAGS is ${MYSQL_CFLAGS}")

execute_process(
    COMMAND ${MYSQL_CONFIG} --libs
    OUTPUT_VARIABLE MYSQL_LIBS
    OUTPUT_STRIP_TRAILING_WHITESPACE
)
message(STATUS "MYSQL_LIBS is ${MYSQL_LIBS}")
# 将 MySQL 标志添加到编译器和链接器选项
include_directories(${MYSQL_CFLAGS})
link_directories(${MYSQL_LIBS})

target_include_directories(main PRIVATE ${MYSQL_CFLAGS})
target_link_libraries(main PRIVATE ${MYSQL_LIBS})
# 设置变量的值
set(MY_VAR "Hello World")
message(STATUS "Variable MY_VAR is ${MY_VAR}")